﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace MxWeiXinPF.Web.shopmgr.coupon
{
    public partial class coupon_setting : MxWeiXinPF.Web.UI.ManagePage
    {
        private Model.wx_uni_account_module moduleset = null;
        private BLL.wx_uni_account_module bmodule = new BLL.wx_uni_account_module();
        MxWeiXinPF.Model.coupon.couponsetting jsonset = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShowInfo();
                TreeBindCoupon();
            }
        }

        private void ShowInfo()
        {
            moduleset = bmodule.GetModuleByUniacid(Common.MyCommFun.GetMainShopID());

            if (moduleset != null)
            {
                jsonset = JsonConvert.DeserializeObject<Model.coupon.couponsetting>(moduleset.settings);
                if (jsonset.enablesendnewuser == 0)
                {
                    this.rdbIsActive.SelectedIndex = 1;
                }
                else
                {
                    this.rdbIsActive.SelectedIndex = 0;
                }

            }
            else {
                moduleset = new Model.wx_uni_account_module();
                moduleset.moule = "morris_coupon";
                moduleset.uniacid = Common.MyCommFun.GetMainShopID();
                moduleset.enabled = 1;
                jsonset = new Model.coupon.couponsetting();
                jsonset.sendnewusercouponid = 0;
                jsonset.enablesendnewuser = 0;
                moduleset.settings = JsonConvert.SerializeObject(jsonset);
                bmodule.Add(moduleset);
                moduleset.id = bmodule.GetMaxId();
            }
        }

        private void TreeBindCoupon()
        {
            BLL.wx_shop_coupon bll = new BLL.wx_shop_coupon();

            Model.manager weixin = base.GetAdminInfo();
            DataSet ds = bll.GetList(" status = 1 and timeend >'" + DateTime.Now.ToString() + "' ");
            this.ddlCouponId.Items.Clear();
            this.ddlCouponId.Items.Add(new ListItem("未选择", "0"));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["couponname"].ToString().Trim();
                this.ddlCouponId.Items.Add(new ListItem(Title, Id));
            }
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (row["id"].ToString() == jsonset.sendnewusercouponid.ToString())
                {
                    ddlCouponId.SelectedValue = row["id"].ToString();
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            moduleset = bmodule.GetModuleByUniacid(Common.MyCommFun.GetMainShopID());
            jsonset = JsonConvert.DeserializeObject<Model.coupon.couponsetting>(moduleset.settings);
            if (jsonset == null)
            {
                JscriptMsg("修改失败！", "coupon_setting.aspx", "Error");
                return;
            }
            jsonset.sendnewusercouponid = Convert.ToInt32(ddlCouponId.SelectedValue);
            jsonset.enablesendnewuser = Convert.ToInt32(rdbIsActive.SelectedValue);
            moduleset.settings = JsonConvert.SerializeObject(jsonset);
            if (bmodule.Update(moduleset))
            {
                JscriptMsg("修改成功！", "coupon_setting.aspx", "Success");
                return;
            }
            else {
                JscriptMsg("修改失败！", "coupon_setting.aspx", "Error");
                return;
            }
        }
    }
}